import axios from 'axios';
import { API_ROOT } from '../config.jsx';

export const FETCH_ARTICLE_DATA = 'FETCH_ARTICLE_DATA';
export const SELECT_ARTICLE_DATA = 'SELECT_ARTICLE_DATA';
export const INIT_SEL_ARTICLE_DATA = 'INIT_SEL_ARTICLE_DATA';
export const FETCH_FAVORITE_DATA = 'FETCH_FAVORITE_DATA';
export const FETCH_APPROVE_DATA = 'FETCH_APPROVE_DATA';
export const FETCH_APPROVE_LIST = 'FETCH_APPROVE_LIST';
export const RESOLVE_APPROVE_DATA = 'RESOLVE_APPROVE_DATA';
export const REJECT_APPROVE_DATA = 'REJECT_APPROVE_DATA';

export function fetchArticle(channelId, page) {
    axios.defaults.headers.common['Authorization'] = sessionStorage.token;
    const info = axios.get(`${API_ROOT}/wechatapi/articles/${channelId}/${page}`);
    return {
        type: FETCH_ARTICLE_DATA,
        payload: info
    };
}

export function fetchApproveArticle(id) {
    axios.defaults.headers.common['Authorization'] = sessionStorage.token;
    const info = axios.get(`${API_ROOT}/wechatapi/ReviewArticle/${id}`);
    return {
        type: FETCH_APPROVE_DATA,
        payload: info
    };
}

export function selectArticle(q, page, channelId,sort) {
    axios.defaults.headers.common['Authorization'] = sessionStorage.token;
    let channelid = parseInt(channelId);
    let sortId = parseInt(sort);
    const info = axios.post(`${API_ROOT}/wechatapi/Search`, { q: q, page: page, channelId: channelid,sortField:sortId });
    return {
        type: SELECT_ARTICLE_DATA,
        payload: info
    };
}

export function initSelectArticle() {
    return {
        type: INIT_SEL_ARTICLE_DATA,
        payload: { articles: [], page: 1 }
    };
}

export function fetchFavoriteArticle(page,key) {
    axios.defaults.headers.common['Authorization'] = sessionStorage.token;
    const info = axios.post(`${API_ROOT}/wechatapi/favorites`,{page:page,q: key});
    return {
        type: FETCH_FAVORITE_DATA,
        payload: info
    };
}

export function fetchApproveArticles() {
    axios.defaults.headers.common['Authorization'] = sessionStorage.token;
    const info = axios.get(`${API_ROOT}/wechatapi/review/list`);
    return {
        type: FETCH_APPROVE_LIST,
        payload: info
    };
}